<?php

namespace App\Http\Controllers\UserCenter;

use App\Http\Models\User;
use App\Http\Models\UserSubmitAudit;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Models\UserAuth;
use App\Http\Models\StatisticsUser;
use Validator;
use Auth;
use DB;

/**
 * 用户放单权限审核
 * Creater: lqr
 */
class UserSubmitAuditController extends Controller
{
    /**
     * 申请审核材料
     * @param Request $request
     * @return string
     */
    public function applyAudit(Request $request)
    {
        $uid = Auth::id();//获取当前登陆用户ID
        if (empty($uid)) {
            return json_encode(['code' => 1, 'msg' => '用户尚未登陆！']);
        }
        $data = $request->only(['industry', 'linker', 'qq', 'phone', 'content']);

        $error = DataValidate($data, [
            'industry' => 'required',
            'linker' => 'required',
            'qq' => 'required|numeric',
            'phone' => 'required|numeric',
            'content' => 'required'
        ]);

        if (count($error) > 0) {
            return json_encode(['code' => 1, 'msg' => $error]);
        }
        $user_submit = UserSubmitAudit::where(['uid' => $uid])->first();
        if (empty($user_submit)) {
            $id = UserSubmitAudit::insert([
                'uid' => $uid,
                'state' => 0,
                'industry' => $data['industry'],
                'linker' => $data['linker'],
                'qq' => $data['qq'],
                'phone' => $data['phone'],
                'content' => $data['content'],
                'created_at' => date('y-m-d h:i:s', time())
            ]);
			if($id){				
				StatisticsUser::addOrderApplyUser();//增加放单用户人数
			}
        } else {
			if($user_submit['state']==1){
				return json_encode(['code' => 1, 'msg' => '审核资料已通过无法修改！']);
			}
            $id = UserSubmitAudit::where(['uid' => $uid])->update([
                'uid' => $uid,
                'state' => 0,
                'industry' => $data['industry'],
                'linker' => $data['linker'],
                'qq' => $data['qq'],
                'phone' => $data['phone'],
                'content' => $data['content'],
                'created_at' => date('y-m-d h:i:s', time())
            ]);
            if($id && $user_submit['state']==2){
                StatisticsUser::addOrderApplyUser();//增加放单用户人数
            }
        }
        if ($id) {
            return json_encode(['code' => 0, 'msg' => 'ok']);
        } else {
            return json_encode(['code' => 1, 'msg' => 'fail']);
        }
    }

    /**
     * 获取用户最近的审核材料
     * @return string
     */
    public function lastApply()
    {
        $uid = Auth::id();//获取当前登陆用户ID
        if (empty($uid)) {
            return json_encode(['code' => 1, 'msg' => '用户尚未登陆！']);
        }
        $datas = UserSubmitAudit::select('state', 'refuse', 'industry', 'linker', 'qq', 'phone', 'content', 'created_at')->where('uid', $uid)->orderBy('id', 'desc')->limit(1)->get();
        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => $datas]);
    }
}
